#include <algorithm>    //使用find()函数要包含该头文件
#include <boost/algorithm/string.hpp>
#include <boost/core/ignore_unused.hpp>

#include "mpHttpParams.h"

#include "mpHttpHeaders.h"


namespace MP{
namespace HTTP{

/*****************************************
 * HttpHeaders
 *****************************************/

HttpHeaders::HttpHeaders()
    :_connection_keepalive(true),
      _cache_control(MP::HTTP::HK_CACHE_CONTROL_NO_CACHE),
      _pragma(MP::HTTP::HK_CACHE_CONTROL_NO_CACHE),
      _expires("-1"),
      _headers()
{

}


void HttpHeaders::addHeader(const std::string & key,
                            const std::string & val){

    if (key.empty() || val.empty()) {
        return;
    }

    if(checkIsCommonHttpHeader(key,val)){
        return;
    }

    std::vector<HttpHeader>::iterator tmp;
    tmp = std::find(_headers.begin(), _headers.end(), key);

    if( tmp != _headers.end() ){
        tmp->value = val;
    }
    else{
        HttpHeader header(key,val);
        _headers.emplace_back(header);
    }
}

void HttpHeaders::clearHeaders() {
    _headers.clear();
}

std::string HttpHeaders::getHeader(const std::string & key){
    if (!key.empty()) {
        std::vector<HttpHeader>::iterator tmp;
        tmp = std::find(_headers.begin(), _headers.end(), key);

        if( tmp != _headers.end() ){
            return tmp->value;
        }
    }
    return "";
}

void HttpHeaders::remove(const std::string & key){
    if (!key.empty()) {
        std::vector<HttpHeader>::iterator tmp;
        tmp = std::find(_headers.begin(), _headers.end(), key);

        if( tmp != _headers.end() ){
            _headers.erase(tmp);
        }
    }
}

const std::vector<HttpHeader> & HttpHeaders::data() const {
    return _headers;
}

void HttpHeaders::setConnectionKeepalive(bool keepalive){
    _connection_keepalive = keepalive;
}

bool HttpHeaders::getConnectionKeepalive() const{
    return _connection_keepalive;
}

void HttpHeaders::setNoCache(){
    _cache_control = MP::HTTP::HK_CACHE_CONTROL_NO_CACHE;
    _pragma = MP::HTTP::HK_CACHE_CONTROL_NO_CACHE;
    _expires = "-1";
}

//例如：setCache_Control("max-age=60")
void HttpHeaders::setCache_Control(const std::string & cache_control) {
    _cache_control = cache_control;
    _expires = "";
    _pragma = "";
}

const std::string& HttpHeaders::getCache_Control() const{
    return _cache_control;
}

const std::string& HttpHeaders::getPragma() const{
    return _pragma;
}

const std::string& HttpHeaders::getExpires() const{
    return _expires;
}

bool HttpHeaders::checkIsCommonHttpHeader(const std::string & key,
                                          const std::string & val){

    if (boost::iequals(key, MP::HTTP::HK_CONNECTION)) {
        setConnectionKeepalive(boost::iequals(val, MP::HTTP::HK_CONNECTION_KEEP_ALIVE));
        return true;
    }

    if (boost::iequals(key, MP::HTTP::HK_CACHE_CONTROL)) {
        if (boost::iequals(val, MP::HTTP::HK_CACHE_CONTROL_NO_CACHE)) {
            setNoCache();
        }
        else{
            setCache_Control(val);
        }
        return true;
    }

    return false;
}

/*****************************************
 * HttpRequestHeaders
 *****************************************/

// Set the Authorization header. E.g. Authorization:hmac id="xxxxxxx", algorithm="hmac-sha1", headers="source x-date", signature="xyxyxyxyxyxy"
void HttpRequestHeaders::setAuthorization(const std::string & authorization) {
    _authorization = authorization;
}

const std::string& HttpRequestHeaders::getAuthorization() const{
    return _authorization;
}

void HttpRequestHeaders::setAccept(const std::string & accept) {
    _accept = accept;
}

const std::string& HttpRequestHeaders::getAccept() const{
    return _accept;
}

void HttpRequestHeaders::setUserAgent(const std::string & userAgent) {
    _userAgent = userAgent;
}

const std::string& HttpRequestHeaders::getUserAgent() const{
    return _userAgent;
}

void HttpRequestHeaders::setAcceptEncoding(const std::string & accept_encoding){
    boost::ignore_unused(accept_encoding);
    //暂时先不支持修改支持的编码
    //_accept_encoding = accept_encoding;
}

const std::string& HttpRequestHeaders::getAcceptEncoding() const{
    return _accept_encoding;
}


bool HttpRequestHeaders::checkIsCommonHttpHeader(const std::string & key,
                                                 const std::string & val) {

    if (boost::iequals(key, MP::HTTP::HK_AUTHORIZATION)) {
        setAuthorization(val);
        return true;
    }

    if (boost::iequals(key, MP::HTTP::HK_ACCEPT)) {
        setAccept(val);
        return true;
    }

    if (boost::iequals(key, MP::HTTP::HK_USERAGENT)) {
        setUserAgent(val);
        return true;
    }

    if (boost::iequals(key, MP::HTTP::HK_ACCEPT_ENCODING)) {
        setAcceptEncoding(val);
        return true;
    }

    return HttpHeaders::checkIsCommonHttpHeader(key,val);
}


}   //HTTP
}   //MP
